﻿Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Json
Module Module1
    Dim divider As String = "-----------------------------------------------------------------------------"
    Dim _WebRequest As System.Net.WebRequest = Nothing
    Dim _WebResponse As System.Net.WebResponse = Nothing
    Sub Main()
        Console.WriteLine(divider)
        Console.ForegroundColor = ConsoleColor.Yellow
        Console.BackgroundColor = ConsoleColor.DarkRed
        Console.WriteLine("QPaste Server Config Query")
        Console.WriteLine("by Silens.")
        Console.ResetColor()
        Console.WriteLine(divider)
        Console.WriteLine()
        commands()
    End Sub
    Sub commands()
        ' The commands part.
        ' Help - Self explanatory.
        ' Try - check the server for 'defaults'
        ' Get - get config
        ' Exit
        Dim command() As String = Nothing
        Dim parameters() As String = Nothing
        Console.Write("QP: ")
        command = Console.ReadLine().Split(" ")
        Select Case command(0)
            Case "help"
                Console.WriteLine("Available commands: help,try,get,exit")
                commands()
            Case "try"
                getdefaults(command(1))
                commands()
            Case "get"
                Dim url As String = command(1)
                If url = "qpb" Then
                    getconf("http://qpb.me/defaults")
                Else : getconf(url)
                End If
                commands()
            Case "exit"
                End
            Case Else
                Console.WriteLine("Unknown command.")
                commands()
        End Select
    End Sub
    Public Function getdefaults(ByVal url As String)
        ' Trying to find the config (defaults)
        If tryurl(url & "/index.php?defaults") = True Then
            Console.WriteLine("Config found: " & url & "/index.php?defaults")
        ElseIf tryurl(url & "/defaults") = True Then
            Console.WriteLine("Config found: " & url & "/defaults")
        Else : Console.WriteLine("No DEFAULTS found.")
        End If
    End Function
    Public Function tryurl(ByVal sourceurl As String)
        ' Looking for "name": in response
        Try
            Dim WR As WebRequest = WebRequest.Create(sourceurl)
            Dim stream As Stream = WR.GetResponse.GetResponseStream()
            Dim reader As New StreamReader(stream)
            Dim line As String = reader.ReadToEnd
            If line.Contains("""name""" & ":") Then
                Return (True)
            End If
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            commands()
        End Try
    End Function
    Public Function getconf(ByVal url As String)
        Try
            Dim wrGETURL As WebRequest
            wrGETURL = WebRequest.Create(url)
            Dim objStream As Stream
            objStream = wrGETURL.GetResponse.GetResponseStream()
            Dim objReader As New StreamReader(objStream)
            Dim sLine As String = ""
            Dim i As Integer = 0
            Dim ret_string As String = Nothing
            Do While Not sLine Is Nothing
                i += 1
                sLine = objReader.ReadLine
                If Not sLine Is Nothing Then
                    ret_string += sLine
                End If
            Loop
            parse(ret_string)
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            commands()
        End Try
        Return (_WebResponse)
    End Function
    Public Function writeuserconf()
        Dim conf As String = _
        "[User Config]" & vbCrLf & _
        "author=Anonymous" & vbCrLf & _
        "email=someguy@somewhere.com" & vbCrLf & _
        "openpaste=False" & vbCrLf & _
        "privatebydeafult=False" & vbCrLf & _
        "defaultpassword="
        Return (conf)
    End Function
    Public Function parse(ByVal text As String)
        Try
            ' Parsing the reply and writing the config
            Console.WriteLine("Writing config...")
            Dim config As String = "config.ini"
            Dim writer As StreamWriter = New StreamWriter(config)
            ' Writing Config header
            writer.Write("[Server Config]" & vbCrLf)
            Dim parser As JsonTextParser = New JsonTextParser
            Dim obj As JsonObjectCollection = parser.Parse(text)
            For Each field As JsonObject In obj
                Dim name As String = field.Name
                Dim value = field.GetValue
                Dim type As String = field.GetValue.GetType.Name
                Select Case type
                    Case "String"
                        value = field.GetValue
                    Case "Double"
                        value = field.GetValue
                    Case "List`1"
                        Dim newstring As String = Nothing
                        For Each item As JsonObject In value
                            Dim n_name As String = item.Name
                            Dim n_value As String = item.GetValue
                            newstring += n_name & ":" & n_value & "|"
                        Next
                        value = newstring
                    Case Else
                        value = field.GetValue.GetType.Name
                End Select
                Console.WriteLine("Writing " & name & "...")
                writer.WriteLine(name & "=" & value)
            Next
            writer.WriteLine()
            writer.WriteLine(writeuserconf())
            writer.Close()
            Console.WriteLine()
            Console.WriteLine(divider)
            Console.WriteLine("Config written, type ""exit"" to exit.")
            commands()
        Catch ex As Exception
            Console.WriteLine(ex.Message)
            commands()
        End Try
    End Function
End Module
